﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace TeacherLeftHand.Model
{
    public class SelectionItem<T>:INotifyPropertyChanged
    {
        #region const

        private const string IsSelectedPropertyName = "IsSelected";
        private const string ItemPropertyName = "Item";

        #endregion

        #region Field

        private bool _isSelected;
        private T _item;

        #endregion

        #region Property
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected) return;
                _isSelected = value;
                OnPropertyChanged(IsSelectedPropertyName);
                OnSelectionChanged();
            }
        }
        public T Item
        {
            get { return _item; }
            set
            {
                if (value.Equals(_item)) return;
                _item = value;
                OnPropertyChanged(ItemPropertyName);
            }
        }
        #endregion
        #region Ctor
        public SelectionItem(T item):this(false,item){}
        public SelectionItem(bool selected, T item)
        {
            this._isSelected = selected;
            this._item = item;
        }
        #endregion
        #region event
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler SelectionChanged;
        #endregion
        #region Event Invoker

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler changed = PropertyChanged;
            if(changed!=null)
            {
                changed(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void OnSelectionChanged()
        {
            EventHandler changed = SelectionChanged;
            if (changed != null) 
            {
                changed(this, EventArgs.Empty); 
            }
        }
        #endregion 
    }
}
